﻿namespace AnyOffice.DAL
{
    using AnyOffice.Model;
    using Microsoft.ApplicationBlocks.Data;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class NewsCommentDao
    {
        public bool Delete(SqlConnection conn, int commentId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("DELETE FROM [dbo].[News_Comment] ");
            builder.Append("WHERE ");
            builder.Append("\t[CommentId] = @CommentId ");
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@CommentId", SqlDbType.Int) };
            commandParameters[0].Value = commentId;
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, builder.ToString(), commandParameters);
            return true;
        }

        public int Insert(SqlConnection conn, NewsCommentData data)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("INSERT INTO [dbo].[News_Comment] ");
            builder.Append("( ");
            builder.Append("\t[NewsId], ");
            builder.Append("\t[UserId], ");
            builder.Append("\t[Content], ");
            builder.Append("\t[ReplyTime] ");
            builder.Append(") ");
            builder.Append("VALUES ");
            builder.Append("( ");
            builder.Append("\t@NewsId, ");
            builder.Append("\t@UserId, ");
            builder.Append("\t@Content, ");
            builder.Append("\t@ReplyTime ");
            builder.Append(") ");
            builder.Append("SELECT SCOPE_IDENTITY() AS RetValue ");
            SqlParameter[] commandParameters = new SqlParameter[4];
            commandParameters[0] = new SqlParameter("@NewsId", SqlDbType.Int);
            commandParameters[0].Value = data.NewsId;
            commandParameters[1] = new SqlParameter("@UserId", SqlDbType.NVarChar, 20);
            commandParameters[1].Value = data.UserId;
            commandParameters[2] = new SqlParameter("@Content", SqlDbType.NText);
            if (data.ContentIsNull)
            {
                commandParameters[2].Value = DBNull.Value;
            }
            else
            {
                commandParameters[2].Value = data.Content;
            }
            commandParameters[3] = new SqlParameter("@ReplyTime", SqlDbType.DateTime);
            commandParameters[3].Value = data.ReplyTime;
            object obj2 = SqlHelper.ExecuteScalar(conn, CommandType.Text, builder.ToString(), commandParameters);
            if (obj2 != null)
            {
                return Convert.ToInt32(obj2);
            }
            return -1;
        }

        public DataTable SelectByNewsId(SqlConnection conn, int newsId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT ");
            builder.Append("\t[News_Comment].[CommentId], ");
            builder.Append("\t[News_Comment].[NewsId], ");
            builder.Append("\t[News_Comment].[UserId], ");
            builder.Append("\t[News_Comment].[Content], ");
            builder.Append("\t[News_Comment].[ReplyTime], ");
            builder.Append("\t[Users].[UserName], ");
            builder.Append("\t[Department].[DeptName] ");
            builder.Append("FROM ");
            builder.Append("\t[dbo].[News_Comment], ");
            builder.Append("\t[dbo].[Users], ");
            builder.Append("\t[dbo].[Department] ");
            builder.Append("WHERE ");
            builder.Append("\t[News_Comment].[NewsId] = @NewsId ");
            builder.Append("\tAND [News_Comment].[UserId] = [Users].[UserId] ");
            builder.Append("\tAND [Users].[DeptId] = [Department].[DeptId] ");
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@NewsId", SqlDbType.Int) };
            commandParameters[0].Value = newsId;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, builder.ToString(), commandParameters);
            set.Tables[0].TableName = "News_Comment";
            return set.Tables[0];
        }
    }
}

